package com.ruoyi.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.regex.Pattern;

@Slf4j
public class SMS {

    public static String[] PREFIXES = {
            "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "145", "147", "149",
            "150", "151", "152", "153", "155", "156", "157", "158", "159", "166", "170", "171", "172",
            "173", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186",
            "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"
    };

    public static String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,2,3,5,6,7,8,9])|(18[0-9])|(19[0,1,2,3,5,6,7,8,9]))\\d{8}$";

    public static String generatePhoneNumber() {
        Random random = new Random();
        Pattern p = Pattern.compile(regex);
        String phone;
        {
            phone = makePhoneNumber(random);
        }
        while (!p.matcher(phone).matches());
        return phone;
    }

    public static String makePhoneNumber(Random random) {
        int prefixIndex = random.nextInt(PREFIXES.length);
        String number = String.format("%08d", random.nextInt(99999999)); // 生成8位随机数
        return PREFIXES[prefixIndex] + number;
    }
}
